home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / rsynth / src / dict.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  1.7 KB  |  97 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <search.h>
  7. #include <stdlib.h>
  8. #include <memory.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/mman.h>
  12. #include "proto.h"
  13. #include "dict.h"
  14.  
  15. #ifndef DICT_PATH
  16. #define DICT_PATH "./text710.dat"
  17. #endif
  18.  
  19. int
  20. dict_init(d, path)
  21. dict_ptr d;
  22. char *path;
  23. {
  24.  int code = 0;
  25.  int fd;
  26.  if (!path || !path[0])
  27.   path = DICT_PATH;
  28.  fd = open(path, O_RDONLY, 0444);
  29.  if (fd >= 0)
  30.   {
  31.    struct stat st;
  32.    if (!fstat(fd, &st))
  33.     {
  34.      d->size = st.st_size;
  35.      d->base = (dictrec_ptr) mmap(0, d->size, PROT_READ, MAP_PRIVATE, fd, 0);
  36.      if (d->base && d->base != (dictrec_ptr) - 1)
  37.       {
  38.        d->entries = d->size / sizeof(dictrec_t);
  39.        code = 1;
  40.       }
  41.     }
  42.    close(fd);
  43.   }
  44.  return code;
  45. }
  46.  
  47. void
  48. dict_term(d)
  49. dict_ptr d;
  50. {
  51.  munmap((caddr_t) d->base, d->size);
  52. }
  53.  
  54. static int spell_cmp PROTO((char *a, char *b));
  55.  
  56. static int
  57. spell_cmp(a, b)
  58. char *a;
  59. char *b;
  60. {
  61.  return strncmp(a, b, 23);
  62. }
  63.  
  64. dictrec_ptr
  65. spell_find(d, s, n)
  66. dict_ptr d;
  67. char *s;
  68. unsigned n;
  69. {
  70.  dictrec_t r;
  71.  dictrec_ptr x;
  72.  char *p = r.spell;
  73.  memset(&r, ' ', sizeof(r));
  74.  while (n-- > 0)
  75.   *p++ = *s++;
  76.  r.eoln = '\n';
  77.  x = (dictrec_ptr) bsearch(&r, d->base, d->entries,
  78.                            sizeof(dictrec_t), spell_cmp);
  79.  if (!x && isupper(r.spell[0]))
  80.   {
  81.    for (p = r.spell; p < r.spell + sizeof(r.spell); p++)
  82.     {
  83.      if (isupper(*p))
  84.       *p = tolower(*p);
  85.     }
  86.    x = (dictrec_ptr) bsearch(&r, d->base, d->entries,
  87.                              sizeof(dictrec_t), spell_cmp);
  88.   }
  89.  if (x)
  90.   {
  91.    /* Find 1st matching entry */
  92.    while (x > d->base && !spell_cmp(r.spell, x[-1].spell))
  93.     x--;
  94.   }
  95.  return x;
  96. }
  97.